package china.accountinghelper.adapter;

import com.xdroid.base.AdapterBase;
import china.wc.tallyhelper.R;
import china.wc.tallyhelper.business.BusinessBook;
import china.wc.tallyhelper.business.BusinessExpense;
import china.wc.tallyhelper.model.ModelBook;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 本类是账本管理页面中，BookListView用的Adapter。
 * 
 * @author 王超
 * 
 */
public class AdapterListViewBookList extends AdapterBase<ModelBook>
{
    public static final int SELECT_ALL     = 0;
    public static final int SELECT_ENABLED = 1;
    private BusinessBook    mBusinessBook = new BusinessBook();
    private BusinessExpense    mBusinessExpense = new BusinessExpense();
    private int             mModel;

    public AdapterListViewBookList(Context pContext, int pModel)
    {
        super(pContext, null);
        mModel = pModel;
        
        if (mModel == SELECT_ALL) {
            setList(mBusinessBook.select());
        } else if (mModel == SELECT_ENABLED) {
            setList(mBusinessBook.selectEnabledBook());
        }

    }
    public AdapterListViewBookList(Context pContext)
    {
        this(pContext, SELECT_ALL);
    }

    @Override
    public long getItemId(int pPosition)
    {
        return getList().get(pPosition).getID();
    }
    
    @Override
    public View getView(int pPosition, View pConvertView, ViewGroup pParent)
    {

        if (pConvertView == null) {
            pConvertView = this.getLayoutInflater().inflate(R.layout.item_listview_booklist, null);
        }

        ModelBook _ModelBookObject = getList().get(pPosition);

        ImageView _ItemListViewBookList_ImageView_Ico = (ImageView) pConvertView.findViewById(R.id.ItemListViewBookList_ImageView_Ico);
        if (_ModelBookObject.getState() == ModelBook.STATE_DEFAULT)
        {
            _ItemListViewBookList_ImageView_Ico.setImageResource(R.drawable.icon_xxjx);
        } else {
            _ItemListViewBookList_ImageView_Ico.setImageResource(R.drawable.icon_xxjx_pxjx);
        }

        TextView _ItemListViewBookList_TextView_Name = (TextView) pConvertView.findViewById(R.id.ItemListViewBookList_TextView_Name);
        _ItemListViewBookList_TextView_Name.setText(_ModelBookObject.getName());

        if (_ModelBookObject.getState() == ModelBook.STATE_DISABLED) {
            _ItemListViewBookList_TextView_Name.setTextAppearance(getContext(), R.style.TextView_ListViewItem_Normal_Red);
        } else if (_ModelBookObject.getState() == ModelBook.STATE_ENABLED) {
            _ItemListViewBookList_TextView_Name.setTextAppearance(getContext(), R.style.TextView_ListViewItem_Normal);
        } else if (_ModelBookObject.getState() == ModelBook.STATE_DEFAULT) {
            _ItemListViewBookList_TextView_Name.setTextAppearance(getContext(), R.style.TextView_ListViewItem_Normal);
        }
        
        TextView _ItemListViewBookList_TextView_Count = (TextView) pConvertView.findViewById(R.id.ItemListViewBookList_TextView_Count);
        _ItemListViewBookList_TextView_Count.setText(String.format(getContext().getString(R.string.message_category_count), mBusinessExpense.howManyExpenses(_ModelBookObject)));
        
        TextView _ItemListViewBookList_TextView_YourSpend = (TextView) pConvertView.findViewById(R.id.ItemListViewBookList_TextView_YourSpend);
        _ItemListViewBookList_TextView_YourSpend.setText(String.format(getContext().getString(R.string.message_category_your_spend), mBusinessExpense.countSelf(_ModelBookObject)));

        TextView _ItemListViewBookList_TextView_OthersSpend = (TextView) pConvertView.findViewById(R.id.ItemListViewBookList_TextView_OthersSpend);
        _ItemListViewBookList_TextView_OthersSpend.setText(String.format(getContext().getString(R.string.message_category_others_spend), mBusinessExpense.countOthers(_ModelBookObject)));
        
        return pConvertView;
    }

    /**
     * 数据库中数据改变后，需要重新setList()，否则调用本方法不会刷新数据。
     */
    @Override
    public void notifyDataSetChanged()
    {
        if (mModel == SELECT_ALL) {
            setList(mBusinessBook.select());
        } else if (mModel == SELECT_ENABLED) {
            setList(mBusinessBook.selectEnabledBook());
        }
        
        super.notifyDataSetChanged();
    }

}
